// utils/request.ts
import Taro from '@tarojs/taro';

const BASE_URL = 'http://127.0.0.1:3000';
interface RequestOptions {
    url: string;
    data?: any;
    header?: Record<string, string>;
}

interface ResponseData {
    code: number;
    data: any;
    message: string;
}

/**
 * 封装POST请求
 * @param options 请求配置
 * @returns Promise<ResponseData>
 */
export const post = (options: RequestOptions): Promise<ResponseData> => {
    return new Promise((resolve, reject) => {
        Taro.request({
            url: BASE_URL + options.url,
            method: 'POST',
            data: options.data || {},
            header: {
                'content-type': 'application/json'
                // ...options.header || {}
            },
            success: (res) => {
                // 这里可以根据后端返回的数据结构进行调整
                // if (res) {
                //     resolve(res.data);
                // } else {
                //     reject(new Error(res.data.message || '请求失败'));
                // }
                resolve(res.data);
            },
            fail: (err) => {
                reject(new Error(err.errMsg || '网络请求失败'));
            }
        });
    });
};
